package com.mars.simple.push.service.impl;

import com.mars.push.core.factory.PushFactory;
import com.mars.push.core.request.PushRequest;
import com.mars.push.core.service.PushService;
import com.mars.simple.push.entity.PushUser;
import com.mars.simple.push.mapper.PushUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author wq
 * @version 1.0
 * @date 2021/03/12 9:58
 */
@Service
public class PushServiceImpl implements com.mars.simple.push.service.PushService {

    @Resource
    private PushUserMapper pushUserMapper;


    @Override
    public void singlePush(PushRequest request) {
        PushUser pushUser = pushUserMapper.selectPushUser(request.getUserId());
        if (Objects.nonNull(pushUser)) {
            request.setRegisterId(pushUser.getRegisterId());
            PushService instance = PushFactory.getInstance((pushUser.getDevice()));
            try {
                if (Objects.nonNull(instance)) {
                    instance.singlePush(request);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
